package design;

import design.mq.Consumer;
import design.mq.Data;
import design.mq.Provider;

import java.util.concurrent.*;

/**
 * Producer-Consumer称为生产者消费者模式，是消息队列的中间件的核心实现模式 ActiveMA/RocketMQ/Kafka/RabbitMQ
 */
public class ProCom {
    public static void main(String[] args) throws InterruptedException {
        // 消息队列
        BlockingQueue<Data> queue = new LinkedBlockingDeque<Data>(10);
        // 生产着
        Provider p1 = new Provider("p1",queue);
        Provider p2 = new Provider("p2",queue);
        Provider p3 = new Provider("p3",queue);

        // 消费者
        Consumer c1 = new Consumer("c1",queue);
        Consumer c2 = new Consumer("c2",queue);
        Consumer c3 = new Consumer("c3",queue);

        ExecutorService caceh = Executors.newFixedThreadPool(6);
        caceh.execute(p1);
        caceh.execute(p2);
        caceh.execute(p3);
        caceh.execute(c1);
        caceh.execute(c2);
        caceh.execute(c3);
        // 创建线程池运行
        Thread.sleep(3000);
        // 生产者停止生产数据
        p1.stop();
        p2.stop();
        p3.stop();
        caceh.shutdown();
    }
}
